const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
var date = new Date();
var app = getApp();

Page({
  data: {
    rootDeptName: '',
    appBgImageUrl: '',
    appBgColor: '',
    appFontColor: '',
    currentRootDeptId:'',
    userInfo: {},
    newGoods: [],
    hotGoods: [],
    topics: [],
    brands: [],
    floorGoods: [],
    banner: [],
    channel: [],
    //中大团队扩展
    goodsCount: 0,
    collageGoods: [],
    futuresGoods: [],
    stockGoods:[],//现货
    time:[],//当前时间,毫秒形式
    app: app,
  },
  onShareAppMessage: function (options) {
    var currentShop = wx.getStorageSync('currentShop');
    return {
      title: this.data.rootDeptName,
      //desc: '专属定制的小程序供货平台',
      path: '/pages/index/index?currentRootDeptId=' + this.data.currentRootDeptId
    }
  }, onPullDownRefresh() {
    // 增加下拉刷新数据的功能
    var self = this;
    self.onShow();
  },
  getIndexData: function () {
    let that = this;
    var timee = date.getTime();
    var currentShop = wx.getStorageSync('currentShop');
    var currentRootDeptId = currentShop.rootDeptId;
    var currentUserGrade = currentShop.userGrade;
    that.setData({
      time:timee,
      rootDeptName: currentShop.rootDeptName,
      appBgImageUrl: currentShop.appBgImageUrl,
      appBgColor: currentShop.appBgColor,
      appFontColor: currentShop.appFontColor,
      currentRootDeptId: currentRootDeptId,
      time: timee
    });
    app.globalData.currentRootDeptId = currentRootDeptId;
    app.globalData.currentUserGrade = currentUserGrade;
    
    util.request(api.GoodsCount, { grade: currentUserGrade, deptId: currentRootDeptId }).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });
    util.request(api.IndexUrlCollageGoods, { grade: currentUserGrade, deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        //计算百分比
        if (res.data.collageGoodsList && res.data.collageGoodsList.length > 0) {
          for (var i = 0; i < res.data.collageGoodsList.length; i++) {
            var collagePercent = parseInt(res.data.collageGoodsList[i].collage_curr_num / res.data.collageGoodsList[i].collage_mini_num * 100);
            if (collagePercent > 100) {
              collagePercent = 100;
            }
            res.data.collageGoodsList[i].collagePercent = collagePercent;
          }
        }
        that.setData({
          collageGoods: res.data.collageGoodsList
        });
      }
    });
    util.request(api.IndexUrlFuturesGoods, { grade: currentUserGrade, deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          futuresGoods: res.data.futuresGoodsList
        });
      }
    });
    util.request(api.IndexUrlNewGoods, { grade: currentUserGrade, deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          newGoods: res.data.newGoodsList
        });
      }
    });
    util.request(api.IndexUrlHotGoods, { grade: currentUserGrade, deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          hotGoods: res.data.hotGoodsList
        });
      }
    });
    //中大团队拓展
    util.request(api.GoodsList, { grade: currentUserGrade, deptId: currentRootDeptId, saleType: "x",size:7})
      .then(function (res) {
        if (res.errno === 0) {
          that.setData({
            stockGoods: res.data.goodsList,
          });
        }
      });
    //util.request(api.IndexUrlTopic).then(function (res) {
    //  if (res.errno === 0) {
    //    data.topics = res.data.topicList
    //  that.setData(data);
    //  }
    //});
    //util.request(api.IndexUrlBrand).then(function (res) {
    //  if (res.errno === 0) {
    //    data.brand = res.data.brandList
    //  that.setData(data);
    //  }
    //});
    util.request(api.IndexUrlCategory, { grade: currentUserGrade, deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          floorGoods: res.data.categoryList
        });
      }
    });
    util.request(api.IndexUrlBanner, { deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          banner: res.data.banner
        });
      }
    });
    util.request(api.IndexUrlChannel, { deptId: currentRootDeptId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          channel: res.data.channel
        });
      }
    });
  },
  contactShop: function(){
    wx.navigateTo({
      url: '/pages/shopDetail/shopDetail',
    })
  },
  onLoad: function (options) {
    let that=this;
    if (options.currentRootDeptId && options.currentRootDeptId.length>0){
      util.request(api.GetCurrentShop, { deptId: options.currentRootDeptId }).then(function (res) {
        wx.setStorageSync('currentShop', res.data.currentShop);
        that.getIndexData();
      });
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    wx.stopPullDownRefresh();
    this.getIndexData();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
})